#include <stdint.h>
#include "config.h"
#include "apitrans.h"
#include "result.h"
#include "devapi.h"


int api_get_key(unsigned char *cmd, int len)
{
	uint32_t time;
	int ret1;
	uint16_t ret;
	uint8_t key = 0;
	uint8_t offset = 0;
	time = BCDtoDec(cmd+offset,4);
	offset += 4;
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	ret1 = kb_get_key(time);
	if(ret1 <= 0){
		ret = GET_KEY_OVERTIME;
		memcpy( cmd, &ret, 2);
		return  -1;
	}
	else{
		key = ret1;
		ret = DEAL_SUCCESS;
	}
	offset = 0;
	memcpy(cmd+offset,&ret,2);
	offset += 2;
	memcpy(cmd+offset,&key, 1);
	offset += 1;
	return offset;
}


#if 0

int get_ascii_string(uint32_t x, uint32_t y, char * buf, uint32_t minlen,
                uint32_t maxlen, uint32_t ms, uint8_t exit_ctrl, uint8_t show)
{
	int remain, key, count = 0;
	uint8_t tmp[256]={0};
	uint8_t flag =1;
	if (buf == NULL) return -1;
	if (maxlen < minlen) return -1;
	if (maxlen == 0) return 0;
	remain = ms;
	memset(buf,0,256 );
	while (1) {
		flag = !flag;
		if(flag && count <= minlen){
			if(show == 0x00){
				buf[count]='_';
				lcd_disp_string(x, y, buf);
			}
			else{
				tmp[count]='_';
				lcd_disp_string(x, y, tmp);
			}
		}
		else{
			if(show == 0x00){
				buf[count]=' ';
				lcd_disp_string(x, y, buf);
			}
			else{
				tmp[count]=' ';
				lcd_disp_string(x, y, tmp);
			}
		}
		key = kb_get_key( 500 );
		if(key <= 0)
		{
			remain = disp_counter(ms, remain);
			if(!remain)
			{
				return -2;
			}
		}else{
			remain = ms;
			clear_disp_counter();
		}

	    if (key == KEY_ESC){ 
			memset( buf, 0, count );		//clear sensitive data
			return -3;
	    }
		else if( key == 0 )
			continue;
	    else if (key == KEY_ENTER) {
            if (count == 0) break;
            if (count >= minlen) break;
	    }
	    else if (key == KEY_BS) {
            if (count == 0) 
				continue;
			tmp[count] = ' ';
			buf[count] = ' ';
            count --;
			tmp[count] = ' ';
			buf[count] = ' ';
	    }
	    else if (key >= '0' && key <= '9') {	
			
			if (count == maxlen) {
				if(exit_ctrl == 0x01)
					break;
				else
					continue;
			}
			if(show == 0x00)
            	buf[count ++] = key;
			else
				tmp[count++] = show;
		}
	}
    return count;
}






int api_get_string(unsigned char *cmd, int len)
{
	uint32_t time;
	uint16_t ret = 0;
	int ret1 ;
	uint8_t key = 0;
	
	uint8_t x;
	uint8_t y;
	uint8_t minlen;
	uint8_t maxlen;
	uint8_t show;
	uint8_t exit_ctrl;
	uint8_t buf[256];
	int offset = 0;
	 
	x = cmd[offset];
	offset += 1;
	y = cmd[offset];
	offset += 1;
	minlen= cmd[offset];
	offset += 1;
	maxlen= cmd[offset];
	offset += 1;
	show= cmd[offset];
	offset += 1;
	exit_ctrl = cmd[offset];
	offset += 1;
	time = BCDtoDec(cmd + offset,4);
	offset += 4;
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	ret1 = get_ascii_string( x, y, buf, minlen, maxlen, time, exit_ctrl, show);

	if(ret1 == -1){
		ret = GET_KEY_FAIL;
		memcpy( cmd, &ret, 2);
		return  -1;
	}
	else if(ret1 == -2){
		ret = GET_KEY_OVERTIME;
		memcpy( cmd, &ret, 2);
		return	-1;
	}else if(ret1 == -3)
	{
		ret = GET_KEY_ESC;
		memcpy( cmd, &ret, 2);
		return	-1;
	}
	offset = 0;
	memcpy(cmd+offset,&ret, 2);
	offset += 2;
	*(cmd + offset) = ret1;
	offset += 1;
	memcpy(cmd + offset,buf, ret1);
	offset += ret1;
	
	return offset;
}

int api_get_amount(unsigned char *cmd, int len)
{

	uint8_t x;
	uint8_t y;
	uint32_t time;
	
	int tmplen,key,i,remain;
	int input = 0;
	uint8_t offset = 0;
	uint16_t ret; 
	uint8_t amtbuf[12];
	uint8_t dispbuf[25];

	x = cmd[offset];
	offset++;
	y = cmd[offset];
	offset++;
	time = BCDtoDec(cmd+offset,4);
	offset += 4;
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd, &ret, 2);
		return -1;
	}
	ret = DEAL_SUCCESS;
	memset(amtbuf, '0', sizeof (amtbuf));
	amtbuf[10] = 0;
	remain = time;
	do {

		tmplen = sprintf(dispbuf, "%llu", atoll(amtbuf) / 100);
		sprintf(dispbuf + tmplen, ".%02d", (int)atoll(amtbuf) % 100);
		lcd_disp_string(x, y, dispbuf);
		key = get_key(500);
		if(key < 0)
		{
			remain = disp_counter(time, remain);
			if(!remain)
			{
				ret = GET_KEY_OVERTIME;
				memcpy( cmd, &ret, 2);
				return -1;
			}

		}else{
			remain = time;
			clear_disp_counter();
		}
		
		if (key == KEY_ESC)
		{
			ret = GET_KEY_ESC;
			memcpy( cmd, &ret, 2);
			return -1;
		}
		if (key == '0' && input == 0) 
			continue;
		if (key >= '0' && key <= '9') 
		{
			if (++ input == 11) {
				break;
			}
			for (i = 0; i < 9; i ++) {
				amtbuf[i] = amtbuf[i + 1];
			}
			amtbuf[9] = key;
		}else if (key == KEY_ENTER) 
		{
			break;
		}else if (key == KEY_BS) 
		{
			for (i = 9; i > 0; i --) {
				amtbuf[i] = amtbuf[i - 1];
			}
			amtbuf[0] = '0';
			if (input > 0) {
				input --;
			}
		}
	}while (1);
	offset = 0;
	memcpy(cmd+offset,&ret, 2);
	offset += 2;
	asc2bcd(amtbuf, 12, cmd + offset, 0);
	offset += 6;
	return offset;
}
#endif

static func_t key_fun[] = {
	NULL, api_get_key
};


int cmd_key(uint8_t cmd, uint8_t *buf, int len)
{
	if(cmd < 0x01 || cmd > FUN_SIZE(key_fun))
		return -5;
	return key_fun[(int)cmd]((uint8_t*)buf, len);
}




